

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Moe Wang">
  <meta name="keywords" content="">
  
    <meta name="description" content="前言 这个效果也是我偶然间在一个视频中看到的，然后开始逐步理清思路动手开始实现，简单来讲就是利用Entity的显示隐藏以及坐标的转换实现。因为是野路子所以可能实现过程略显粗糙或冗余，接下来来我们开始。 基本思路我们需要利用点击事件获取我们选中的这个实体，隐藏当前实体并获取到它的外围坐标集来生成新的多边形，并且要对这个多边形进行放大平移等操作。同时我们需要提供一个关闭按钮让其能够退出所谓的分层模式。">
<meta property="og:type" content="article">
<meta property="og:title" content="【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五）">
<meta property="og:url" content="https://moewang0321.github.io/2021/06/03/%E3%80%90%E4%B8%89%E7%BB%B4GIS%E5%8F%AF%E8%A7%86%E5%8C%96%E3%80%91%E5%9F%BA%E4%BA%8EVue+Cesium+Supermap%E5%AE%9E%E7%8E%B0%E6%99%BA%E6%85%A7%E5%9F%8E%E5%B8%82%EF%BC%88%E4%BA%94%EF%BC%89/index.html">
<meta property="og:site_name" content="moe_大杂烩">
<meta property="og:description" content="前言 这个效果也是我偶然间在一个视频中看到的，然后开始逐步理清思路动手开始实现，简单来讲就是利用Entity的显示隐藏以及坐标的转换实现。因为是野路子所以可能实现过程略显粗糙或冗余，接下来来我们开始。 基本思路我们需要利用点击事件获取我们选中的这个实体，隐藏当前实体并获取到它的外围坐标集来生成新的多边形，并且要对这个多边形进行放大平移等操作。同时我们需要提供一个关闭按钮让其能够退出所谓的分层模式。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/949a196a072149f9bd16b5006e014ce8~tplv-k3u1fbpfcp-zoom-crop-mark:3024:3024:3024:1702.awebp">
<meta property="article:published_time" content="2021-06-03T00:51:55.000Z">
<meta property="article:modified_time" content="2023-02-14T08:44:37.413Z">
<meta property="article:author" content="Moe Wang">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="Cesium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/949a196a072149f9bd16b5006e014ce8~tplv-k3u1fbpfcp-zoom-crop-mark:3024:3024:3024:1702.awebp">
  
  
  
  <title>【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五） - moe_大杂烩</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"moewang0321.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>moe_大杂烩</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-06-03 08:51" pubdate>
          2021年6月3日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          34 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五）</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/cd9637ac16fa465d88ec4ece08f0ae58~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" lazyload alt="分层展示.gif"></p>
<p>这个效果也是我偶然间在一个视频中看到的，然后开始逐步理清思路动手开始实现，简单来讲就是利用Entity的显示隐藏以及坐标的转换实现。因为是野路子所以可能实现过程略显粗糙或冗余，接下来来我们开始。</p>
<h3 id="基本思路"><a href="#基本思路" class="headerlink" title="基本思路"></a>基本思路</h3><p>我们需要利用点击事件获取我们选中的这个实体，隐藏当前实体并获取到它的外围坐标集来生成新的多边形，并且要对这个多边形进行放大平移等操作。同时我们需要提供一个关闭按钮让其能够退出所谓的分层模式。</p>
<p>我们如何对一个坐标集进行操作让它所代表的多边形能够缩放或平移呢？这就要用到我们的地理分析库<code>Turf.js</code>了，关于Turf的介绍及基本使用方法我在<a target="_blank" rel="noopener" href="https://juejin.cn/post/6968626897156603918">Turf.js—让你在浏览器上实现地理分析</a>里做了相关介绍，需要的小伙伴可以进行浏览。</p>
<p>关闭按钮该如何实现呢？还记得前面的文章中我们进行的简单的文本标记类封装吗，道理是一样的，将我们指定的div元素渲染到Cesium的容器中，并在类中指定所需的逻辑、方法等即可。</p>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><h4 id="分层效果"><a href="#分层效果" class="headerlink" title="分层效果"></a>分层效果</h4><p>思路清晰后我们可以开始功能实现。同样的，我们可以先实现功能后对其封装，在本文中我们只贴出关键代码，小伙伴们可以读完文章后亲自动手实现。</p>
<p>首先我们肯定是加载一个<code>entity</code>实体，这里老生常谈，我就不多做说明了，然后我们需要注册一个点击事件，用来触发点击实体后的响应函数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">let</span> handler = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Cesium</span>.<span class="hljs-title class_">ScreenSpaceEventHandler</span>(viewer.<span class="hljs-property">scene</span>.<span class="hljs-property">canvas</span>);<br><br>handler.<span class="hljs-title function_">setInputAction</span>(<span class="hljs-function">(<span class="hljs-params">movement</span>) =&gt;</span> &#123;<br>    <span class="hljs-keyword">let</span> pick = viewer.<span class="hljs-property">scene</span>.<span class="hljs-title function_">pick</span>(movement.<span class="hljs-property">position</span>);<br>    <span class="hljs-keyword">if</span> (<span class="hljs-title class_">Cesium</span>.<span class="hljs-title function_">defined</span>(pick)) &#123;<br>        <span class="hljs-keyword">if</span>(!pick.<span class="hljs-property">id</span>.<span class="hljs-property">isFloor</span>) &#123;<br>            <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">showFloor</span>(pick.<span class="hljs-property">id</span>);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">selectFloor</span>(pick.<span class="hljs-property">id</span>);<br>        &#125;<br>    &#125;<br>&#125;, <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">ScreenSpaceEventType</span>.<span class="hljs-property">LEFT_CLICK</span>);<br></code></pre></td></tr></table></figure>

<p>在响应函数中我们首先隐藏点击的这个实体，为我们的分层实体集腾位置，接着获取到这个实体的外围坐标集，并用它作为我们的分层实体的坐标集。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">showFloor</span>(<span class="hljs-params">entity</span>) &#123;<br>    viewer.<span class="hljs-property">entities</span>.<span class="hljs-title function_">getById</span>(entity.<span class="hljs-property">id</span>).<span class="hljs-property">show</span> = <span class="hljs-literal">false</span>;<br>    <span class="hljs-keyword">let</span> height = <span class="hljs-number">30</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; entity.<span class="hljs-property">floor</span>; i++) &#123;<br>        <span class="hljs-keyword">let</span> floor = &#123;<br>            <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;floor&quot;</span> + i,<br>            <span class="hljs-attr">id</span>: entity.<span class="hljs-property">id</span> + <span class="hljs-string">&quot;F&quot;</span> + i,<br>            <span class="hljs-attr">isFloor</span>: <span class="hljs-literal">true</span>,<br>            <span class="hljs-attr">floor</span>: i + <span class="hljs-number">1</span>,<br>          	<span class="hljs-attr">positions</span>: entity.<span class="hljs-property">positions</span>,<br>            <span class="hljs-attr">polygon</span>: &#123;<br>                <span class="hljs-comment">// hierarchy: Cesium.Cartesian3.fromDegreesArray(floorPos),</span><br>                <span class="hljs-attr">hierarchy</span>: entity.<span class="hljs-property">polygon</span>.<span class="hljs-property">hierarchy</span>.<span class="hljs-property">_value</span>,<br>                <span class="hljs-attr">material</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">YELLOW</span>.<span class="hljs-title function_">withAlpha</span>(<span class="hljs-number">0.4</span>),<br>                height,<br>                <span class="hljs-attr">outline</span>: <span class="hljs-literal">true</span>,<br>                <span class="hljs-attr">ouelineColor</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">BLACK</span>,<br>            &#125;,<br>        &#125;;<br>        height += <span class="hljs-number">60</span>;<br>        viewer.<span class="hljs-property">entities</span>.<span class="hljs-title function_">add</span>(floor);<br>    &#125;<br>&#125;,<br></code></pre></td></tr></table></figure>

<p>这样我们基本的分层效果就做出来了。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7dc64dfc11b4473e8822fa53baf471a2~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" lazyload alt="分层展示1.gif"></p>
<h4 id="选中效果"><a href="#选中效果" class="headerlink" title="选中效果"></a>选中效果</h4><p>接下来我们实现选中效果，选中效果我们可以使用高亮、平移、放大以及它们的组合展示，高亮这里不做阐述，效果在我们网格实现那篇文章已经实现，本文主要实现放大效果。</p>
<p>想实现放大效果我们需要利用<code>Turf.js</code>进行坐标的变换，所以我们首先在项目中安装Turf</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cmd">npm i @turf/turf<br></code></pre></td></tr></table></figure>

<p>我们需要利用<code>Turf.js</code>中的<code>polygon</code>和<code>transformScale</code>实现。为了方便，在最开始<code>Entity</code>生成的时候我将它的二维坐标组作为一个属性绑定到实体上，以便后续使用。在实际项目中根据具体的情况进行调整，比如利用三维坐标转换或根据实体<code>ID</code>调取接口获取等。</p>
<p>我们想使用Turf就要遵循它的格式。所以首先我们需要生成Turf承认的Polygon实体，然后将其进行放大或缩小并获取其放大后的坐标数组。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">let</span> poly = turf.<span class="hljs-title function_">polygon</span>([positions]);<br><span class="hljs-keyword">let</span> translatedPoly = turf.<span class="hljs-title function_">transformScale</span>(poly, <span class="hljs-number">1.4</span>);<br><span class="hljs-keyword">let</span> transPoints = translatedPoly.<span class="hljs-property">geometry</span>.<span class="hljs-property">coordinates</span>[<span class="hljs-number">0</span>];<br></code></pre></td></tr></table></figure>

<p>然后生成一个实体并添加到实体集中即可。</p>
<p>这里只是做了最基本的实现，完整的思考下来其实会发现还有很多需要完善的地方。比如：点击已选中楼层后取消选中、选中楼层的唯一性等等。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ac587743c20942a3aadc5fd6fe8285d3~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" lazyload alt="分层展示2.gif"></p>
<p>完整方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">selectFloor</span>(<span class="hljs-params">floor</span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(floor);<br>    <span class="hljs-keyword">let</span> positions = floor.<span class="hljs-property">positions</span>;<br>    <span class="hljs-keyword">let</span> poly, translatedPoly, transPoints, floorPosSingle = [];<br>    poly = turf.<span class="hljs-title function_">polygon</span>([positions]);<br>    translatedPoly = turf.<span class="hljs-title function_">transformScale</span>(poly, <span class="hljs-number">1.4</span>);<br>    transPoints = translatedPoly.<span class="hljs-property">geometry</span>.<span class="hljs-property">coordinates</span>[<span class="hljs-number">0</span>];<br>    transPoints.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> &#123;<br>        floorPosSingle.<span class="hljs-title function_">push</span>(item[<span class="hljs-number">0</span>], item[<span class="hljs-number">1</span>]);<br>    &#125;);<br><br>    <span class="hljs-keyword">let</span> selectFloor = &#123;<br>        <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;select_floor&quot;</span>,<br>        <span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;active_&#x27;</span> + floor.<span class="hljs-property">id</span>,<br>        <span class="hljs-attr">isFloor</span>: <span class="hljs-literal">true</span>,<br>        <span class="hljs-attr">isActive</span>: <span class="hljs-literal">true</span>,<br>        <span class="hljs-attr">polygon</span>: &#123;<br>            <span class="hljs-attr">hierarchy</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegreesArray</span>(floorPosSingle),<br>            <span class="hljs-attr">material</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">RED</span>.<span class="hljs-title function_">withAlpha</span>(<span class="hljs-number">0.4</span>),<br>            <span class="hljs-attr">height</span>: floor.<span class="hljs-property">polygon</span>.<span class="hljs-property">height</span>.<span class="hljs-property">_value</span>,<br>            <span class="hljs-attr">outline</span>: <span class="hljs-literal">true</span>,<br>            <span class="hljs-attr">outlineColor</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Color</span>.<span class="hljs-property">BLACK</span>,<br>        &#125;,<br>    &#125;;<br><br>    viewer.<span class="hljs-property">entities</span>.<span class="hljs-title function_">getById</span>(floor.<span class="hljs-property">id</span>).<span class="hljs-property">show</span> = <span class="hljs-literal">false</span><br>    viewer.<span class="hljs-property">entities</span>.<span class="hljs-title function_">add</span>(selectFloor)<br>&#125;,<br></code></pre></td></tr></table></figure>

<h4 id="关闭按钮"><a href="#关闭按钮" class="headerlink" title="关闭按钮"></a>关闭按钮</h4><p>关闭按钮这里也不做过多说明，和动态文本标记的思路大致相同，唯一的区别是现在的DOM中绑定一个点击事件，需要通过这个事件触发删除分层实体集和恢复显示楼层。这里我简单说明一下如何在封装的类中调用vue文件下的方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// class类中</span><br><span class="hljs-variable language_">this</span>.<span class="hljs-property">_vmInstance</span>.<span class="hljs-property">closeEvent</span> = <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>();<br>&#125;;<br><br><span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">_vmInstance</span>.<span class="hljs-property">show</span> = <span class="hljs-literal">false</span>; <span class="hljs-comment">//删除dom</span><br>    <span class="hljs-comment">// do something……</span><br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs vue">// vue文件中<br>&lt;div :id=&quot;id&quot; class=&quot;close-container&quot; v-if=&quot;show&quot; @click=&quot;closeClick&quot;&gt;&lt;/div&gt;<br><br>closeClick() &#123;<br>    if (this.closeEvent) &#123;<br>    	this.closeEvent();<br>    &#125;<br>&#125;,<br></code></pre></td></tr></table></figure>

<h4 id="特殊情况"><a href="#特殊情况" class="headerlink" title="特殊情况"></a>特殊情况</h4><p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/20d92729e36846688dfe0fe31e94a514~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" lazyload alt="分层展示3.gif"></p>
<p>有时候我们会发现，我们的实体并不是一个完整的多边形体，有可能是环状或带洞的多边形，这时候利用上述的方法会发现疯狂报错。原因是无论是Ceisum中还是Turf中，带洞多边形的坐标格式都和普通的多边形是不一样的，下面简单说明一下两个库中带洞多边形的生成方式。</p>
<h5 id="Cesium"><a href="#Cesium" class="headerlink" title="Cesium"></a>Cesium</h5><p>在Ceisum中，<code>entity.polygon.hierarchy</code>作为多边形的外围坐标集，提供了两种写法，上文中的写法为简写，默认不带洞，而带洞多边形则需要提供一个对象，其中包含两个字段<code>positions</code>和<code>holes</code>。顾名思义，一个是外围坐标集，一个是洞坐标集。其中<code>holes</code>可以提供多个坐标集，相当于挖多个洞。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">let</span> entity = &#123;<br>    <span class="hljs-attr">polygon</span>:&#123;<br>        <span class="hljs-attr">hierarchy</span>:&#123;<br>            <span class="hljs-attr">positions</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegreesArray</span>(outPos),<br>            <span class="hljs-attr">holes</span>: [<br>                &#123; <span class="hljs-attr">positions</span>: <span class="hljs-title class_">Cesium</span>.<span class="hljs-property">Cartesian3</span>.<span class="hljs-title function_">fromDegreesArray</span>(inPos) &#125;<br>            ]<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h5 id="Turf-js"><a href="#Turf-js" class="headerlink" title="Turf.js"></a>Turf.js</h5><p>而在Turf中，我们无法实现直接掏洞，只能曲线救国，将洞的坐标集和外围坐标集生成多边形并分别进行放大操作，获取坐标后进行我们想要的实现。</p>
<h3 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h3><p>这个例子结合了前面的封装DOM类以及点击事件的运用，例子虽然粗糙但是能够帮我们巩固相关知识。这篇文章也会收录到我的专栏中，里面也有其他效果的实现，感兴趣的小伙伴可以点击订阅。如果有好的建议或提醒欢迎评论区浏览或添加联系方式共同学习进步。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Cesium/" class="category-chain-item">Cesium</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaScript/">#JavaScript</a>
      
        <a href="/tags/Cesium/">#Cesium</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五）</div>
      <div>https://moewang0321.github.io/2021/06/03/【三维GIS可视化】基于Vue+Cesium+Supermap实现智慧城市（五）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Moe Wang</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年6月3日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/06/07/Cesium%E7%9A%84Property%E6%9C%BA%E5%88%B6%E7%A9%B6%E7%AB%9F%E6%9C%89%E5%A4%9A%E9%A6%99/" title="Cesium的Property机制究竟有多香">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Cesium的Property机制究竟有多香</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/06/02/Cesium%E6%BA%90%E7%A0%81%E8%B7%9F%E8%AF%BB%E4%B9%8BCesiumWidget%E7%9A%84%E5%AE%9E%E7%8E%B0/" title="Cesium源码跟读之CesiumWidget的实现">
                        <span class="hidden-mobile">Cesium源码跟读之CesiumWidget的实现</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
    <div id="giscus" class="giscus"></div>
    <script type="text/javascript">
      Fluid.utils.loadComments('#giscus', function() {
        var options = {"repo":"moewang0321/moewang0321.github.io","repo-id":"MDEwOlJlcG9zaXRvcnkyMDEyNDI0NTY=","category":"Show and tell","category-id":"DIC_kwDOC_63WM4CUMsH","theme-light":"light_protanopia","theme-dark":"dark_dimmed","mapping":"pathname","reactions-enabled":1,"emit-metadata":1,"input-position":"top","loading":"lazy","lang":"zh-CN"};
        var attributes = {};
        for (let option in options) {
          if (!option.startsWith('theme-')) {
            var key = option.startsWith('data-') ? option : 'data-' + option;
            attributes[key] = options[option];
          }
        }
        var light = 'light_protanopia';
        var dark = 'dark_dimmed';
        window.GiscusThemeLight = light;
        window.GiscusThemeDark = dark;
        attributes['data-theme'] = document.documentElement.getAttribute('data-user-color-scheme') === 'dark' ? dark : light;
        for (let attribute in attributes) {
          var value = attributes[attribute];
          if (value === undefined || value === null || value === '') {
            delete attributes[attribute];
          }
        }
        var s = document.createElement('script');
        s.setAttribute('src', 'https://giscus.app/client.js');
        s.setAttribute('crossorigin', 'anonymous');
        for (let attribute in attributes) {
          s.setAttribute(attribute, attributes[attribute]);
        }
        var ss = document.getElementsByTagName('script');
        var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
        e.parentNode.insertBefore(s, e.nextSibling);
      });
    </script>
    <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
